package com.zhiche.lisa.bms.controller.prepay;


import com.baomidou.mybatisplus.plugins.Page;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.zhiche.lisa.bms.dao.model.fee.FeePayable;
import com.zhiche.lisa.bms.dao.model.prepay.PrepayBill;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.prepay.PrepayBillPertrolVO;
import com.zhiche.lisa.bms.service.prepay.PrepayBillService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 预付账单 前端控制器
 * </p>
 *
 * @author zhangkun
 * @since 2018-09-20
 */
@RestController
@RequestMapping("/prepayBill")
@Api(value = "/prepayBill", description = "预付账单", tags = "预付账单")
public class PrepayBillController {
    public Logger LOGGER = LoggerFactory.getLogger(PrepayBillController.class);
    @Autowired
    private PrepayBillService prepayBillService;

    /**
     * 查询预付账单
     *
     * @param page
     * @return
     */
    @PostMapping("/queryPrepayBillPage")
    @ApiOperation(value = "/queryPrepayBillPageApi", notes = "查询预付账单", response = RestfulResponse.class)
    public RestfulResponse<Page<PrepayBill>> queryPrepayBillPage(@RequestBody Page page) {
        LOGGER.info("controller:prepayBill/queryPrepayBillPage data:{}", page);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            Page prepayBill = prepayBillService.queryPrepayBillPage(page);
            result.setData(prepayBill);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 查询预付账单
     *
     * @param condition
     * @return
     */
    @PostMapping("/queryPrepayBillList")
    @ApiOperation(value = "/queryPrepayBillListApi", notes = "查询预付账单", response = RestfulResponse.class)
    public RestfulResponse<List<PrepayBill>> queryPrepayBillList(@RequestBody Map<String, Object> condition) {
        LOGGER.info("controller:prepayBill/queryPrepayBillPage data:{}", condition);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            List prepayBill = prepayBillService.queryPrepayBillList(condition);
            result.setData(prepayBill);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 查询预付账单-针对打印
     *
     * @param condition
     * @return
     */
    @PostMapping("/queryPrepayBillListForPrint")
    @ApiOperation(value = "/queryPrepayBillListForPrint", notes = "查询预付账单-针对打印", response = RestfulResponse.class)
    public RestfulResponse<Object> queryPrepayBillListForPrint(@RequestBody Map<String, Object> condition) {
        LOGGER.info("PrepayBillController.queryPrepayBillListForPrint data:{}", condition);
        RestfulResponse<Object> result = new RestfulResponse(0, "成功", null);
        try {
            result.setData(prepayBillService.queryPrepayBillForPrint(condition));
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @GetMapping("/queryPrepayBillAppoint")
    @ApiOperation(value = "/queryPrepayBillAppointApi", notes = "查询预付账单指定数据", response = RestfulResponse.class)
    public RestfulResponse<List<Object>> queryPrepayBillAppoint(@RequestParam("type") String type, @RequestParam("val") String val) {
        LOGGER.info("controller:prepayBill/queryPrepayBillAppoint data:{}", type);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            List<Object> prepayBill = prepayBillService.selectPrepayBillAppoint(type, val);
            result.setData(prepayBill);
        } catch (Exception e) {
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 生成预付账款
     *
     * @param feePayable
     * @return
     */
    @PostMapping("/insertPrepayBill")
    @ApiOperation(value = "insertPrepayBillApi", notes = "生成预付账单", response = RestfulResponse.class)
    public RestfulResponse<PrepayBill> insertPrepayBill(@RequestBody FeePayable feePayable) {
        LOGGER.info("controller:/PrepayBill/insertPrepayBill data : {}", feePayable);
        RestfulResponse<PrepayBill> result = new RestfulResponse<>(0, "成功", null);
        try {
            prepayBillService.insertPrepayBill(feePayable, null);
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.insertPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("PrepayBillController.insertPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }

    /**
     * 审核预付账单
     *
     * @param idParam
     * @return
     */
    @PostMapping("/checkPrepayBill")
    @ApiOperation(value = "checkPrepayBillApi", notes = "审核预付账单", response = RestfulResponse.class)
    public RestfulResponse<List<String>> checkPrepayBill(@RequestBody CommonVO<Long> idParam) {
        LOGGER.info("controller:/PrepayBill/checkPrepayBill data : {}", idParam);
        RestfulResponse<List<String>> result = new RestfulResponse<>(0, "成功", null);
        try {
            if (idParam.getTypeVal() == null) {
                throw new BaseException("请指定预付审核方式");
            } else {
                if (!ArrayUtils.isEmpty(idParam.getIds())) {
                    ArrayList<Long> ids = Lists.newArrayList(idParam.getIds());
                    if ("oil".equals(idParam.getTypeVal())) {
                        prepayBillService.checkPrepayBill(ids, 0);
                    } else if ("cash".equals(idParam.getTypeVal())) {
                        prepayBillService.checkPrepayBill(ids, 1);
                    }
                }
            }
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.checkPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("PrepayBillController.checkPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }

    /**
     * 确认预付账单
     *
     * @param commonVO
     * @return
     */
    @PostMapping("/confirmPrepayBill")
    @ApiOperation(value = "confirmPrepayBillApi", notes = "确认预付账单", response = RestfulResponse.class)
    public RestfulResponse<PrepayBill> confirmPrepayBill(@RequestBody CommonVO<Long> commonVO) {
        LOGGER.info("controller:/PrepayBill/confirmPrepayBill data : {}", commonVO);
        RestfulResponse<PrepayBill> result = new RestfulResponse<>(0, "成功", null);
        try {
            prepayBillService.confirmPrepayBill(commonVO.getEntityParams(), commonVO.getTypeVal());
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.confirmPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("PrepayBillController.confirmPrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }

    /**
     * 更新预付账单
     *
     * @param ids
     * @return
     */
    @PostMapping("/updatePrepayBill")
    @ApiOperation(value = "updatePrepayBillApi", notes = "更新预付账单", response = RestfulResponse.class)
    public RestfulResponse<PrepayBill> updatePrepayBill(@RequestBody List<Long> ids) {
        LOGGER.info("controller:/PrepayBill/updatePrepayBill data : {}", ids);
        RestfulResponse<PrepayBill> result = new RestfulResponse<>(0, "成功", null);
        try {
            prepayBillService.updatePrepayBill(ids);
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.updatePrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PrepayBillController.updatePrepayBill: {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        }
        return result;
    }

    @PostMapping(value = "/queryPayApplyList")
    @ApiOperation(value = "根据账单ID，查询预付账单", notes = "根据账单ID，查询预付账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<PrepayBill>> queryPayApplyList(@RequestBody List<Long> headIds) {
        RestfulResponse<List<PrepayBill>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<PrepayBill> resultData = prepayBillService.queryPayApplyAreConfirmList(headIds);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/PrepayBillController.queryPayApplyList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PrepayBillController.queryPayApplyList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/getPayApplyByPayIDList")
    @ApiOperation(value = "根据支付申请ID，查询预付账单", notes = "根据账单ID，查询预付账单", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<PrepayBill>> getPayApplyByPayIDList(@RequestBody Long payId) {
        RestfulResponse<List<PrepayBill>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<PrepayBill> resultData = prepayBillService.getPayApplyByPayIDList(payId);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/PrepayBillController.getPayApplyByPayIDList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PrepayBillController.getPayApplyByPayIDList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/updateStateByFcPay")
    @ApiOperation(value = "根据油卡支付的考核状态对应修改预付帐单状态", notes = "根据油卡支付的考核状态对应修改预付帐单状态", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> updateStateByFcPay(@RequestBody List<Long> payBillId) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            prepayBillService.updateStateByFcPay(payBillId);
        } catch (BaseException e) {
            LOGGER.error("/PrepayBillController.updateStateByFcPay ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PrepayBillController.updateStateByFcPay ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    /**
     * 查询第一桶油数据
     *
     * @param page
     * @return
     */
    @PostMapping("/queryPertrolOilPage")
    @ApiOperation(value = "/queryPertrolOilPage", notes = "查询第一桶油数据", response = RestfulResponse.class)
    public RestfulResponse<Page<PrepayBillPertrolVO>> queryPertrolOilPage(@RequestBody Page<PrepayBillPertrolVO> page) {
        LOGGER.info("PrepayBillController.queryPertrolOilPage data:{}", page);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            result.setData(prepayBillService.queryPertrolOilPage(page));
        } catch (Exception e) {
            LOGGER.info("PrepayBillController.queryPertrolOilPage error:{}", e.getMessage());
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    /**
     * 根据客户运单号获取信息
     *
     * @param prepayBillPertrolVO
     * @return
     */
    @PostMapping("/getPrepayBillPertrolOilCondition")
    @ApiOperation(value = "/getPrepayBillPertrolOilCondition", notes = "根据客户运单号获取信息", response = RestfulResponse.class)
    public RestfulResponse<PrepayBillPertrolVO> getPrepayBillPertrolOilCondition(@RequestBody PrepayBillPertrolVO prepayBillPertrolVO) {
        LOGGER.info("PrepayBillController.queryPertrolOilPage data:{}", prepayBillPertrolVO);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            result.setData(prepayBillService.getPertrolOilCondition(prepayBillPertrolVO));
        } catch (Exception e) {
            LOGGER.info("PrepayBillController.getPrepayBillPertrolOilCondition error:{}", e.getMessage());
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }


    /**
     * 确认加油
     *
     * @param prepayBill
     * @return
     */
    @PostMapping("/updatePertrolOilStatue")
    @ApiOperation(value = "/updatePertrolOilStatue", notes = "确认加油", response = RestfulResponse.class)
    public RestfulResponse<Object> updatePertrolOilStatue(@RequestBody PrepayBill prepayBill) {
        LOGGER.info("PrepayBillController.updatePertrolOilStatue data:{}", prepayBill);
        RestfulResponse result = new RestfulResponse(0, "成功", null);
        try {
            prepayBillService.updatePertrolOilStatue(prepayBill);
        } catch (Exception e) {
            LOGGER.info("PrepayBillController.updatePertrolOilStatue error:{}", e.getMessage());
            result.setCode(-1);
            result.setMessage(e.getMessage());
        }
        return result;
    }

    @PostMapping(value = "/updatePertrolOilPrice")
    @ApiOperation(value = "获取并更新加油站油价", notes = "获取并更新加油站油价", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> updatePertrolOilPrice(@RequestBody List<Long> preBillIdList) {
        LOGGER.info("PrepayBillController.updatePertrolOilPrice param: {}", preBillIdList);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            prepayBillService.updatePertrolOilPrice(preBillIdList);
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.updatePertrolOilPrice ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PrepayBillController.updatePertrolOilPrice ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }


    /**
     * 更新第一桶油价
     *
     * @param condition
     * @return
     */
    @ApiOperation(value = "第一桶油充值预付导出", notes = "第一桶油充值预付导出", httpMethod = "POST")
    @PostMapping("/updateBatchPertrolOilPriceByCondition")
    public RestfulResponse<String> updateBatchPertrolOilPriceByCondition(@RequestBody Map<String, Object> condition) {
        LOGGER.info("PrepayBillController.updateBatchPertrolOilPriceByCondition condition: {}", condition);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            prepayBillService.updateBatchPertrolOilPriceByCondition(condition);
        } catch (BaseException e) {
            LOGGER.error("PrepayBillController.updateBatchPertrolOilPriceByCondition ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PrepayBillController.updateBatchPertrolOilPriceByCondition ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}